﻿using Core.SimpleTemp.Common;
using Core.Toolkit.AutoMapperExtend.DataDictionary;
using Core.Toolkit.AutoMapperExtend.DataDictionary.Enum;
using Core.Toolkit.BaseLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Core.Toolkit.AutoMapperExtend
{
    /// <summary>
    /// 
    /// </summary>
    [AutoDi(typeof(IDataDicProvider))]
    public class EnumDataDicProvider : IDataDicProvider
    {
        public EnumDataDicProvider()
        {

        }
        public static string assemblyNames = "Core.Toolkit;" +
            "Core.SimpleTemp.Application;" +
            "Core.SimpleTemp.Entitys;" +
            "Core.SimpleTemp.Repositories;" +
            "Core.SimpleTemp.Api";
        public void LoadData(Dictionary<string, string> _dataDic, Dictionary<string, List<DataDicModel>> _dataDicListByCategory)
        {
            ReflectRegistry(_dataDic, _dataDicListByCategory);
        }



        public static void ReflectRegistry(Dictionary<string, string> _dataDic, Dictionary<string, List<DataDicModel>> _dataDicListByCategory)
        {
            if (!String.IsNullOrEmpty(assemblyNames))
            {
                var arrAssemb = assemblyNames.Split(';');
                //循环程序集
                foreach (var assemblyName in arrAssemb)
                {
                    Assembly assembly = Assembly.Load(assemblyName);
                    List<Type> ts = assembly.GetTypes().ToList();
                    var enumTypes = ts.Where(s => s.IsEnum && s.GetCustomAttributes<EnumDicAttribute>() != null && s.GetCustomAttributes<EnumDicAttribute>().Any());
                    //循环每个程序程序集里面的 EnumDicAttribute 标签对象
                    foreach (var enumType in enumTypes)
                    {
                        Registry(_dataDic, _dataDicListByCategory, enumType);
                    }
                }
            }
        }

        public static void Registry(Dictionary<string, string> _dataDic, Dictionary<string, List<DataDicModel>> _dataDicListByCategory, Type enumType)
        {
            var enumDicAttribute = enumType.GetCustomAttributes<EnumDicAttribute>().FirstOrDefault();
            var categoryCode = enumDicAttribute.CategoryCode;
            if (categoryCode == null)
                categoryCode = enumType.Name;
            EnumDicType enumDicType = enumDicAttribute.EnumDicType;
            bool useIntVlaue = enumDicType == EnumDicType.TypeInt;
            List<DataDicModel> dataDicModels = new List<DataDicModel>();
            foreach (var value in Enum.GetValues(enumType))
            {
                var enumIntValue = Convert.ToInt32(value);
                var enumStringValue = value.ToString();

                string enumDescription = EnumExtension.GetEnumDescription(enumType, enumStringValue);
                if (useIntVlaue)
                {
                    _dataDic.TryAdd($"{categoryCode}_{enumIntValue}", enumDescription);
                    dataDicModels.Add(new DataDicModel
                    {
                        Category = categoryCode,
                        Key = enumIntValue.ToString(),
                        Value = enumDescription
                    });
                }
                else
                {
                    _dataDic.TryAdd($"{categoryCode}_{enumStringValue}", enumDescription);
                    dataDicModels.Add(new DataDicModel
                    {
                        Category = categoryCode,
                        Key = enumStringValue,
                        Value = enumDescription
                    });
                }

            }

            _dataDicListByCategory.Add(categoryCode, dataDicModels);

        }


    }
}
